//
//  GameEngine.h
//  iMine
//
//  Created by gaoxiang on 09-3-8.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>

#define MINE_LEVEL1			1
#define MINE_LEVEL2			2
#define MINE_LEVEL3			3
#define MINE_LEVEL_CUSTOM	4

// 默认级别行列定义
#define MINE_LEVEL1_ROWS	9
#define MINE_LEVEL1_COLS	9
#define MINE_LEVEL1_NUMBER	10

#define MINE_LEVEL2_ROWS	16
#define MINE_LEVEL2_COLS	16
#define MINE_LEVEL2_NUMBER	40

#define MINE_LEVEL3_ROWS	16
#define MINE_LEVEL3_COLS	30
#define MINE_LEVEL3_NUMBER	99

#define MineFieldWidth	20
#define MineFieldHeight 20
#define MineFieldNumberWidth	16
#define MineFieldNumberHeight	16

#define MINEFIELD_KEY_ZERO		@"0"
#define MINEFIELD_KEY_ONE		@"1"
#define MINEFIELD_KEY_TWO		@"2"
#define MINEFIELD_KEY_THREE		@"3"
#define MINEFIELD_KEY_FOUR		@"4"
#define MINEFIELD_KEY_FIVE		@"5"
#define MINEFIELD_KEY_SIX		@"6"
#define MINEFIELD_KEY_SEVEN		@"7"
#define MINEFIELD_KEY_EIGHT		@"8"
#define MINEFIELD_KEY_NINE		@"9"

#define NUMBER_KEY_MINUS		@"red-"
#define NUMBER_KEY_ZERO			@"red0"
#define NUMBER_KEY_ONE			@"red1"
#define NUMBER_KEY_TWO			@"red2"
#define NUMBER_KEY_THREE		@"red3"
#define NUMBER_KEY_FOUR			@"red4"
#define NUMBER_KEY_FIVE			@"red5"
#define NUMBER_KEY_SIX			@"red6"
#define NUMBER_KEY_SEVEN		@"red7"
#define NUMBER_KEY_EIGHT		@"red8"
#define NUMBER_KEY_NINE			@"red9"
#define NUMBER_KEY_INACTIVE		@"Inactive_red"

#define MINEFIELD_KEY_BLOCK		@"block"
#define MINEFIELD_KEY_MINE		@"mine"
#define MINEFIELD_KEY_MINECRASH @"mcrash"
#define MINEFIELD_KEY_BLANK		@"blank"
#define MINEFIELD_KEY_FLAG		@"flag"
#define MINEFIELD_KEY_REDX		@"redx"

enum {
	MineCommandNormal,
	MineCommandSetFlag,
	MineCommandTip,
	MineCommandCancelTip,
	MineCommandFinishTip
};

enum {
	OK,
	Win,
	Lose
};

enum FieldState{
	FSMineField = 1 << 0,
	FSChecked = 1 << 1,
	FSFlag = 1 << 2,
	FSTipping = 1 << 3
};

typedef struct _IntPoint {
    int x;
    int y;
} IntPoint;

NS_INLINE IntPoint GEMakeIntPoint(int x, int y) {
    IntPoint p;
    p.x = x;
    p.y = y;
    return p;
}

@interface GameEngine : NSObject {

	BOOL gameStarted;
	int elapseSecond;
	NSImage *materialImage;
	NSMutableDictionary* materialCache;
	NSTimer *secondTimer;
	
	int nonMineFieldCheckedCount;
	NSImage *mineFieldImage;
	NSImage *secondConterImage;
	NSImage *remainConterImage;
	int flagMineCount;
	int level;
	int rows;
	int cols;
	int mineNumber;
	BOOL gameIsOver;
	
	int **fieldData;
	int currentCols;
	int currentRows;
	NSArray *minePositions;
}
@property(readwrite) int level, rows, cols, elapseSecond, mineNumber;
@property(retain) NSMutableDictionary *materialCache;
@property(retain) NSImage *materialImage;
@property(retain) NSImage *mineFieldImage;
@property(retain) NSImage *secondConterImage;
@property(retain) NSImage *remainConterImage;
@property(readonly) BOOL gameStarted, gameIsOver;

#pragma mark 静态方法
+ (GameEngine *)sharedGameEngine;

#pragma mark 初始化方法
- (id)initAndLoadResources;

#pragma mark 属性操作
- (int)flagMineCount;
- (void)setFlagMineCount:(int)count;
- (void)increaseFlagMineCount;
- (void)decreaseFlagMineCount;
- (int) getElapseSecond;
- (void) increaseElapseSecond:(id)object;

#pragma mark 游戏控制
- (void)testMineField;
- (void) newGame;
- (void) newCustomGame:(int)row withColumn:(int)col withMineNumber:(int)number;
- (void)setGameOver:(BOOL)isWin;

- (void) resetStopWatch;
- (void) turnOnStopWatch;
- (void) turnOffStopWatch;

#pragma mark 雷区初始化
- (NSArray *)randomMine;
- (void) mineInField:(NSSize) size;
- (void)resetFieldDataWithWidth:(int)width andHeight:(int)height;

#pragma mark 雷区回收
- (void)releaseFieldData;

#pragma mark 雷区单个方格等坐标处理
- (void)setAroundMineCountByCoordinate:(int)count withXAxis:(int)x andYAxis:(int)y;
- (int)getAroundMineCountAtPoint:(int)x andYAxis:(int)y;
- (void)signToField:(int)sign atXAxis:(int)x andYAxis:(int)y toCancel:(BOOL)cancel;
- (BOOL)isSignForField:(int)sign atXAxis:(int)x andYAxis:(int)y;
- (BOOL)isPointInSafeBound:(int)x andYAxis:(int)y;
- (NSString *)imageKeyForPoint:(int)x andYAxis:(int)y;

#pragma mark 处理用户动作
- (int) hitTestPosition:(NSPoint)pos withCommand:(int)command;
- (void)hitNormal:(IntPoint)pos;
- (void)hitFlag:(IntPoint)pos;
- (int)hitTip:(IntPoint)pos toCancel:(BOOL)cancel;
- (void)hitTipFinish:(IntPoint)pos flags:(int)flagCount;

#pragma mark 帮助方法
- (IntPoint)convertToMineFieldPosition:(NSPoint)pos;
- (void)postNewGameNotification;
- (void)postNotificationForKey:(NSString *)key;
- (void)postGameStatusNotification:(int)status;
- (NSArray *)minesAroundMineItem:(IntPoint)pos includeChecked:(BOOL)include;
- (NSArray *)minesAroundMineItemIncludeSelf:(IntPoint)pos includeChecked:(BOOL)include;

#pragma mark 图片渲染方法
- (void)renderMineForPoint:(IntPoint)pos toCheck:(BOOL)checked;
- (void)renderToggleFlagForMine:(IntPoint)pos;
- (void)renderToggleTipForMine:(IntPoint)pos toCancel:(BOOL)cancel;



// 改造代码


@end
